/*
	* Publisher Host (c) Creative Commons 2006
	* http://creativecommons.org/licenses/by-sa/2.5/
	* Author: Dustin Diaz | http://www.dustindiaz.com
	* Reference: http://www.dustindiaz.com/basement/publisher.html
*/
/*
	* @Class: Publisher
		* Requires:
			* Array.prototype.forEach
			* Array.prototype.some
			* See Sugar Arrays at: 
			* http://www.dustindiaz.com/basement/sugar-arrays.html
	* Constructor: The almighty holder of its subscribers
*/
function Publisher() {
	this.subscribers = [];
}
/*
	* @desc: deliver | the delivery method
	* @param: data | The data you want to send to your subscribers
	* @param: thisObj | The scope you want to execute your callbacks
*/
Publisher.prototype.deliver = function(data, thisObj) {
	var scope = thisObj || window;
	this.subscribers.forEach(
		function(el) {
			el.call(scope, data);
		}
	);
};
/*
	* @desc: subscribe | Gives all function objects 
	* the ability to subscribe to a Publisher Object
	* @param: publisher | The Publisher Object you wish to subscribe to
*/
Function.prototype.subscribe = function(publisher) {
	var that = this;
	var alreadyExists = publisher.subscribers.some(
		function(el) {
			if ( el === that ) {
				return;
			}
		}
	);
	if ( !alreadyExists ) {
		publisher.subscribers.push(this);
	}
	return this;
};
/*
	* @desc: unsubscribe | Gives all function objects 
	* the ability to "unsubscribe" to a Publisher Object
	* @param: publisher | The Publisher Object you wish to unsubscribe to
*/
Function.prototype.unsubscribe = function(publisher) {
	var that = this;
	publisher.subscribers = publisher.subscribers.filter(
		function(el) {
			if ( el !== that ) {
				return el;
			}
		}
	);
	return this;
};